package com.seele.tigerwallet.ui.activtiy;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.seele.tigerwallet.R;
import com.seele.tigerwallet.model.AssetsModel;
import com.seele.tigerwallet.model.TokenInfoModel;
import com.seele.tigerwallet.ui.base.TitleBaseActivity;
import com.seele.tigerwallet.ui.event.AssetsEvent;
import com.seele.tigerwallet.ui.event.MessageEvent;
import com.seele.tigerwallet.ui.event.WalletEvent;
import com.seele.tigerwallet.ui.manager.DaoManager;
import com.seele.tigerwallet.ui.utils.GsonUtil;
import com.seele.tigerwallet.ui.utils.SPUtils;
import com.seele.tigerwallet.utils.KeyBoardUtils;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.io.Serializable;
import java.sql.SQLException;
import java.util.List;

public class ManageWalletInfoActivity extends TitleBaseActivity implements View.OnClickListener {


    private TextView mWalletNameTv;
    private ImageView mNameEdtImg;
    private TextView mTokeAddress;
    private EditText mEditText;

    private String mAddress;
    private boolean isEdit;
    private TextView mTountTv;
    private TextView mCompanyTv;

    @Override
    public int getContainerViewId() {
        return R.layout.activity_manage_wallet_info;
    }

    @Override
    protected void initView() {
        mWalletNameTv = $f(R.id.wallet_name_tv);
        mNameEdtImg = $f(R.id.name_edt_img);
        mTokeAddress = $f(R.id.toke_address);
        mEditText = $f(R.id.name_edt);
        mTountTv = $f(R.id.toke_count_tv);
        mCompanyTv = $f(R.id.token_company_tv);


        mEditText.setFocusable(false);
        mEditText.setFocusableInTouchMode(false);
        mEditText.clearFocus();

        mNameEdtImg.setOnClickListener(this);
        mEditText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });
        Bundle bundle = getIntent().getExtras();
        if (null != bundle) {
            TokenInfoModel tokenInfoModel = (TokenInfoModel) bundle.getSerializable(ManageWalletInfoActivity.class.getSimpleName());
            if (null != tokenInfoModel) {
                if (!TextUtils.isEmpty(tokenInfoModel.name)) {
//                    String name = String.format(getResources().getString(R.string.new_price), tokenInfoModel.name);
                    setTitle(tokenInfoModel.name);
                    mEditText.setText(tokenInfoModel.name);
                    mEditText.setHintTextColor(getResources().getColor(R.color.color_99FFFFFF));
//                    mWalletNameTv.setText(name);
                } else {
                    setTitle(getResources().getString(R.string.wallet_name));

                }

                if (!TextUtils.isEmpty(tokenInfoModel.address)) {
                    mAddress = tokenInfoModel.address;
                    mTokeAddress.setText(tokenInfoModel.address);
                }

//                mTountTv.setText(SPUtils.getTotal(tokenInfoModel.total));
                if (SPUtils.isCNY()){
                    mCompanyTv.setText(getResources().getString(R.string.company_cny));
                }else {
                    mCompanyTv.setText(getResources().getString(R.string.company_usd));
                }



                if (!TextUtils.isEmpty(tokenInfoModel.address)) {
                    mAddress = tokenInfoModel.address;
                    mTokeAddress.setText(tokenInfoModel.address);
                }
            }
        }

    }


    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.name_edt_img:
                if (!isEdit) {
                    mEditText.setFocusable(true);
                    mEditText.setFocusableInTouchMode(true);
                    mEditText.requestFocus();
                    KeyBoardUtils.openKeybord(mEditText, this);
                    String content = mEditText.getText().toString();
                    if (!TextUtils.isEmpty(content)) {

                        mEditText.setSelection(content.length());
                    }
                    isEdit = true;
                } else {
                    mEditText.setFocusable(false);
                    mEditText.setFocusableInTouchMode(false);
                    mEditText.clearFocus();
                    KeyBoardUtils.closeKeybord(mEditText, this);
                    isEdit = false;
                    final String name = mEditText.getText().toString();
                    if (!TextUtils.isEmpty(name) && !TextUtils.isEmpty(mAddress)) {
                        try {
                            new Thread(new Runnable() {
                                @Override
                                public void run() {
                                    try {
                                        DaoManager.getTokenDao().updateRaw(getSql(name, mAddress));
                                    } catch (Exception e) {
                                        e.printStackTrace();
                                    }
                                }
                            }).start();
                            EventBus.getDefault().post(new WalletEvent(name, mAddress));
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                }

                break;
        }

    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    public String getSql(String name, String address) {
        return String.format(" UPDATE t_token SET name = '%s' WHERE address='%s'", name, address);
    }

}
